package com.tianhui.consignortwofei.mvp.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.CommonUtil;
import com.fgs.common.utils.LogUtil;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.manager.SharedPreferenceManager;
import com.tianhui.consignortwofei.mvp.model.UserInfoModel;
import com.tianhui.consignortwofei.mvp.model.enty.ConsignorInfo;
import com.tianhui.consignortwofei.mvp.model.enty.LoginInfo;
import com.tianhui.consignortwofei.mvp.ui.adapter.ConsignorListAdapter;
import com.xuexiang.xutil.net.JsonUtil;

import org.yczbj.ycrefreshviewlib.inter.OnItemClickListener;
import org.yczbj.ycrefreshviewlib.item.RecycleViewItemLine;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * @author fgs
 * @date 2021/1/21
 * @description
 **/
public class ConsignorListActivity extends BaseActivity {


    @BindView(R.id.activity_consignor_list_recyclerView)
    RecyclerView mRecyclerView;
    private ConsignorListAdapter mAdapter;
    private ArrayList<ConsignorInfo> mConsignorList;
    private UserInfoModel mUserInfoModel;

    @Override
    public int getLayoutId() {
        return R.layout.activity_consignor_list;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public void init() {
        setTitle("切换货权");


        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            mConsignorList = bundle.getParcelableArrayList(KeyValue.KEY_CONSIGNOR_LIST);
        }

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new ConsignorListAdapter(this);
        mRecyclerView.setAdapter(mAdapter);

        RecycleViewItemLine line = new RecycleViewItemLine(this,
                LinearLayoutManager.HORIZONTAL,
                (int) CommonUtil.convertDpToPixel(1),
                getResources().getColor(R.color.colorLineGray));
        mRecyclerView.addItemDecoration(line);

        if (mConsignorList != null && mConsignorList.size() > 0) {
            mAdapter.addAll(mConsignorList);
        }

        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Object object = mAdapter.getAllData().get(position);
                if (object != null && object instanceof ConsignorInfo) {
                    ConsignorInfo info = (ConsignorInfo) object;
                    requestConsignorInfo(info.useId);
                }
            }
        });
    }

    private void requestConsignorInfo(String id) {
        mUserInfoModel = new UserInfoModel();
        Map<String, String> map = new HashMap<>();
        map.put("userId", id);
        mUserInfoModel.consignorInfoModel(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    LoginInfo info = JsonUtil.fromJson(JsonUtil.toJson(commonResponse.getData()), LoginInfo.class);
                    if (info != null) {
                        SharedPreferenceManager.getInstance().setToken(info.token);
                        EventBusManage.refreshConsignorInfo();
                        EventBusManage.refreshOrderList();
                        EventBusManage.goYunDan();

                        finish();
                    }else {
                        showToast("未获取到货权数据");
                    }

                } else if (commonResponse.getCode() == 9999){
                    showToast("无须切换货权");
                }else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }



    private ArrayList<ConsignorInfo> mConsignorInfoList;
    //货权选择列表
    private void requestConsignorGroupListInfo(String id) {
        if (TextUtils.isEmpty(id)) return;
        Map<String, String> map = new HashMap<>();
        map.put("userId", id);
        mUserInfoModel.consignorGroupListModel(getContext(), map, false, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<ArrayList<ConsignorInfo>>>() {
            @Override
            public void onNext(CommonResponse<ArrayList<ConsignorInfo>> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    mConsignorInfoList = commonResponse.getData();
                    if (mConsignorInfoList != null && mConsignorInfoList.size() > 0) {

                        mConsignorList=mConsignorInfoList;

                    }
                } else {
                    LogUtil.print(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                LogUtil.print(e.message);
            }
        });
    }



}
